package com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningLine.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningLine.repository.HddcWyGeologicalsvyplanninglineNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningLine.repository.HddcWyGeologicalsvyplanninglineRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningLine.repository.entity.HddcWyGeologicalsvyplanninglineEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningLine.service.HddcWyGeologicalsvyplanninglineService;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningLine.viewobjects.HddcWyGeologicalsvyplanninglineQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningLine.viewobjects.HddcWyGeologicalsvyplanninglineVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangping
 * @date 2020-12-01
 */
@Service
public class HddcWyGeologicalsvyplanninglineServiceImpl implements HddcWyGeologicalsvyplanninglineService {

	@Autowired
    private HddcWyGeologicalsvyplanninglineRepository hddcWyGeologicalsvyplanninglineRepository;
    @Autowired
    private HddcWyGeologicalsvyplanninglineNativeRepository hddcWyGeologicalsvyplanninglineNativeRepository;

    @Override
    public JSONObject queryHddcWyGeologicalsvyplanninglines(HddcWyGeologicalsvyplanninglineQueryParams queryParams ,
                                                            int curPage, int pageSize) {
        Page<HddcWyGeologicalsvyplanninglineEntity> hddcWyGeologicalsvyplanninglinePage =
                this.hddcWyGeologicalsvyplanninglineNativeRepository.queryHddcWyGeologicalsvyplanninglines(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeologicalsvyplanninglinePage);
        return jsonObject;
    }


    @Override
    public HddcWyGeologicalsvyplanninglineEntity getHddcWyGeologicalsvyplanningline(String id) {
        HddcWyGeologicalsvyplanninglineEntity hddcWyGeologicalsvyplanningline = this.hddcWyGeologicalsvyplanninglineRepository.findById(id).orElse(null);
         return hddcWyGeologicalsvyplanningline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvyplanninglineEntity saveHddcWyGeologicalsvyplanningline(HddcWyGeologicalsvyplanninglineEntity hddcWyGeologicalsvyplanningline) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeologicalsvyplanningline.setId(uuid);
        hddcWyGeologicalsvyplanningline.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvyplanningline.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcWyGeologicalsvyplanningline.setIsValid("1");
        this.hddcWyGeologicalsvyplanninglineRepository.save(hddcWyGeologicalsvyplanningline);
        return hddcWyGeologicalsvyplanningline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvyplanninglineEntity updateHddcWyGeologicalsvyplanningline(HddcWyGeologicalsvyplanninglineEntity hddcWyGeologicalsvyplanningline) {
        hddcWyGeologicalsvyplanningline.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvyplanningline.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeologicalsvyplanninglineRepository.save(hddcWyGeologicalsvyplanningline);
        return hddcWyGeologicalsvyplanningline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeologicalsvyplanninglines(List<String> ids) {
        List<HddcWyGeologicalsvyplanninglineEntity> hddcWyGeologicalsvyplanninglineList = this.hddcWyGeologicalsvyplanninglineRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeologicalsvyplanninglineList) && hddcWyGeologicalsvyplanninglineList.size() > 0) {
            for(HddcWyGeologicalsvyplanninglineEntity hddcWyGeologicalsvyplanningline : hddcWyGeologicalsvyplanninglineList) {
//                this.hddcWyGeologicalsvyplanninglineRepository.delete(hddcWyGeologicalsvyplanningline);
                hddcWyGeologicalsvyplanningline.setIsValid("0");
                hddcWyGeologicalsvyplanningline.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcWyGeologicalsvyplanningline.setUpdateTime(new Date());
                this.hddcWyGeologicalsvyplanninglineRepository.save(hddcWyGeologicalsvyplanningline);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeologicalsvyplanningline(HddcWyGeologicalsvyplanninglineQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeologicalsvyplanninglineNativeRepository.queryHddcWyGeologicalsvyplanningline(queryParams);
        return bigInteger;
    }

}
